package dead;

import java.net.*;

import android.util.Log;

public class UDPSender {
	private int port;
	private InetAddress ipaddr;
	
	public UDPSender(int port, String ip) {
		try {
			ipaddr = InetAddress.getByName(ip);
		} 
		catch (UnknownHostException e) {
			Log.e("UDPError", e.getMessage());
		}
		
	}
	
	public void ping(long delay) {
		DatagramSocket socket;
		try {
			socket = new DatagramSocket(port, ipaddr);
		} 
		catch (SocketException e) {
			Log.e("UDPError", e.getMessage());
			return;
		}
		byte[] sendBuffer = ("Hello Wedley!").getBytes();
		DatagramPacket packet = new DatagramPacket(sendBuffer, sendBuffer.length, ipaddr, port);
		
		try {
		while (true) {
				socket.send(packet);
				Thread.sleep(delay);
			}
		}
		catch (Exception e){
			Log.e("UDPError", e.getMessage());
			socket.close();
		}
	}
	
	
	
}
